package com.offcn.question.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

import com.offcn.question.entity.QmsTypeEntity;
import com.offcn.question.service.QmsTypeService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.offcn.question.entity.QmsQuestionEntity;
import com.offcn.question.service.QmsQuestionService;
import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author yaoshang
 * @email wuqian@gmail.com
 * @date 2021-07-21 19:43:14
 */
@RestController
@RequestMapping("question/question")
public class QmsQuestionController {
    @Autowired
    private QmsQuestionService qmsQuestionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = qmsQuestionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		QmsQuestionEntity question = qmsQuestionService.getById(id);

        return R.ok().put("question", question);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody QmsQuestionEntity Question){
		qmsQuestionService.save(Question);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody QmsQuestionEntity qmsQuestion){
		qmsQuestionService.updateById(qmsQuestion);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		qmsQuestionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 题目上传导入
     */
    @PostMapping("/upload")
    public R upload(MultipartFile file){
        Map map = qmsQuestionService.importExcel(file);
        return R.ok()
                .put("result",map.get("result"))
                .put("msg",map.get("msg"))
                .put("num",map.get("num"));
    }

    /**
     * 题目导出
     */
    @GetMapping("/download")
    public void export(String tableName, HttpServletResponse response){
        Workbook workbook = qmsQuestionService.exportExcel();
        if (workbook!=null){
            String fileName = "uxue_"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".xlsx";
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            //设置文件的格式和字符编码  GB2312设置中文
            response.setContentType("application/octet-stream;charset=GB2312");
            response.setCharacterEncoding("GB2312");
            OutputStream outputStream;
            try {
                outputStream=response.getOutputStream();
                workbook.write(outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                response.getWriter().print("error");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Resource
    private QmsTypeService typeService;
    ////获取按照题库分类的统计数据
    @RequestMapping("/count")
    public R countType(){
        List<Map<String, Object>> mapList = qmsQuestionService.countType();
        //根据分类ID去读取对应的分类数据
        for (Map<String, Object> map : mapList) {
            Long typeId = (Long) map.get("TYPE");
            QmsTypeEntity typeEntity = typeService.getById(typeId);
            //重新封装
            map.put("name",typeEntity.getType());
        }
        return R.ok().put("mapList",mapList);
    }


}
